package com.apps.ftapp.activity;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;

import android.view.View;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.ComModel;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.view.SuperEditText;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;


public class NickNameActivity extends BaseActivity {

    private TextView save;
    private SuperEditText nickname;
    private Gson gson = new Gson();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nick_name);
        save = findViewById(R.id.save_nick);
        nickname = findViewById(R.id.nickname);
        setTitle("修改昵称");
        init();
    }


    private void init() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("nickname");
        nickname.setText(name);
        nickname.setSelection(nickname.getText().length());
//        setEditTextInhibitInputSpeChat(nickname);
//        setbanemoji(nickname);
        nickname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String editable = nickname.getText().toString();
                String str = stringFilter(editable);
                if (!editable.equals(str)) {
                    nickname.setText(str);
                    //设置新的光标所在位置
                    nickname.setSelection(str.length());

                } else {
                    Toasty.normal(getApplicationContext(), "仅支持输入中文、英文、数字");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                //如果EditText中的数据不为空，且长度大于指定的最大长度
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveinfo();
            }
        });
    }

    private void saveinfo() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("nickname", nickname.getText().toString().trim());
        Async.post("settings/nickname", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    ComModel result = gson.fromJson(new String(responseBody), ComModel.class);
                    if (result.getSuccess() == 1) {
                        Toasty.normal(getApplicationContext(), "昵称修改成功！").show();
                        finish();
                    } else {
                        Toasty.normal(getApplicationContext(), result.getError()).show();
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toasty.normal(getApplicationContext(), "接口数据错误").show();
            }
        });


    }


    public static String stringFilter(String str) throws PatternSyntaxException {
        // 只允许字母、数字和汉字
        String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }


}
